<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Exception;
use think\facade\Validate;

class System extends Controller
{
    // 系统信息
    public function systemInfo()
    {
        if (request()->isAjax()) {
            $data = [
                'id' => input('id'),
                'web_name' => input('web_name'),
                'copyright' => input('copyright'),
                'logo' => input('logo')
            ];
            $validate = Validate::make([
                'web_name|网站名称' => 'require',
                'copyright|版权信息' => 'require',
                'logo|网站LOGO' => 'require'
            ]);
            if (!$validate->check($data)) {
                $this->result('', 0, $validate->getError(), 'json');
            } else {
                $updateData = [
                    'id' => $data['id'],
                    'web_name' => $data['web_name'],
                    'copyright' => $data['copyright'],
                    'logo' => $data['logo']
                ];
                Db::startTrans();
                try {
                    model('System')::update($updateData);
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    $this->result('', 0, $e->getMessage(), 'json');
                }
                $this->result('', 1, '操作成功', 'json');
            }
        }
        $systemInfo = model('System')->find();
        $viewData = [
            'systemInfo' => $systemInfo
        ];
        $this->assign($viewData);
        return view();
    }

    // 菜单列表
    public function menuList()
    {
        if (request()->isPost()) {
            $menuList = model('AuthRule')->order('sort', 'asc')->select()->toArray();
            $menuList = $this->recursion($menuList, 0, 0);
            foreach ($menuList as $k => $v) {
                $menuList[$k]['title'] = $v['title'];
            }
            $this->result(['list' => $menuList, 'count' => count($menuList)], 0, '获取分页数据成功', 'json');
        }
        return view();
    }

    // 菜单设置
    public function menuSet()
    {
        $data = [
            'id' => input('id'),
            'sort' => input('sort'),
            'is_show' => input('is_show')
        ];
        $updateData = $rules = [];
        $updateData['id'] = $data['id'];
        if ($data['sort']) {
            $rules = array_merge($rules, [
                'id|菜单' => 'require',
                'sort|排序' => 'require|number'
            ]);
            $updateData['sort'] = $data['sort'];
        }
        if ($data['is_show'] !== null) {
            $rules = array_merge($rules, [
                'id|菜单' => 'require',
                'is_show|显示' => 'require'
            ]);
            $updateData['is_show'] = $data['is_show'];
        }
        $validate = Validate::make($rules);
        if (!$validate->check($data)) {
            $this->result('', 0, $validate->getError(), 'json');
        } else {
            Db::startTrans();
            try {
                model('AuthRule')::update($updateData);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->result('', 0, $e->getMessage(), 'json');
            }
            $this->result('', 1, '操作成功', 'json');
        }
    }

    // 添加菜单
    public function addMenu()
    {
        if (request()->isAjax()) {
            $data = [
                'pid' => input('pid', 0),
                'title' => input('title'),
                'name' => input('name'),
                'icon' => input('icon'),
                'sort' => input('sort'),
                'is_show' => input('is_show', 0)
            ];
            $validate = Validate::make([
                'title|菜单名称' => 'require|unique:auth_rule',
                'name|菜单地址' => 'unique:auth_rule',
                'icon|图标' => 'require',
                'sort|排序' => 'require|number'
            ]);
            if (!$validate->check($data)) {
                $this->result('', 0, $validate->getError(), 'json');
            } else {
                $addData = [
                    'pid' => $data['pid'],
                    'title' => $data['title'],
                    'name' => $data['name'],
                    'icon' => $data['icon'],
                    'sort' => $data['sort'],
                    'is_show' => $data['is_show']
                ];
                Db::startTrans();
                try {
                    model('AuthRule')::create($addData);
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    $this->result('', 0, $e->getMessage(), 'json');
                }
                $this->result('', 1, '操作成功', 'json');
            }
        }
        $oneMenuList = model('AuthRule')::where(['pid' => 0])->order('sort', 'asc')->select();
        $viewData = [
            'oneMenuList' => $oneMenuList
        ];
        $this->assign($viewData);
        return view();
    }

    // 编辑菜单
    public function editMenu()
    {
        if (request()->isAjax()) {
            $data = [
                'id' => input('id'),
                'pid' => input('pid', 0),
                'title' => input('title'),
                'name' => input('name'),
                'icon' => input('icon'),
                'sort' => input('sort'),
                'is_show' => input('is_show', 0)
            ];
            $validate = Validate::make([
                'title|菜单名称' => 'require|unique:auth_rule',
                'name|菜单地址' => 'unique:auth_rule',
                'icon|图标' => 'require',
                'sort|排序' => 'require|number'
            ]);
            if (!$validate->check($data)) {
                $this->result('', 0, $validate->getError(), 'json');
            } else {
                $updateData = [
                    'id' => input('id'),
                    'pid' => $data['pid'],
                    'title' => $data['title'],
                    'name' => $data['name'],
                    'icon' => $data['icon'],
                    'sort' => $data['sort'],
                    'is_show' => $data['is_show']
                ];
                Db::startTrans();
                try {
                    model('AuthRule')::update($updateData);
                    Db::commit();
                } catch (Exception $e) {
                    Db::rollback();
                    $this->result('', 0, $e->getMessage(), 'json');
                }
                $this->result('', 1, '操作成功', 'json');
            }
        }
        $oneMenuList = model('AuthRule')::where(['pid' => 0])->order('sort', 'asc')->select();
        $menuInfo = model('AuthRule')::get(input('id'));
        $viewData = [
            'oneMenuList' => $oneMenuList,
            'menuInfo' => $menuInfo
        ];
        $this->assign($viewData);
        return view();
    }

    // 删除菜单
    public function delMenu()
    {
        Db::startTrans();
        try {
            model('AuthRule')::destroy(input('id'));
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->result('', 0, $e->getMessage(), 'json');
        }
        $this->result('', 1, '操作成功', 'json');
    }

    // 递归菜单
    private function recursion($data, $pid = 0)
    {
        static $arr = [];
        foreach ($data as $k => $v) {
            if ($v['pid'] == $pid) {
                $arr[] = $v;
                $this->recursion($data, $v['id']);
            }
        }
        return $arr;
    }
}
